// pages/accination-planDetail/accination-planDetail.js
const util = require('../../utils/util.js')
import * as utils from '../../utils/util.js'
import request from '../../utils/http.js'
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    minDate: new Date(2000, 1, 1).getTime(),
    maxDate: new Date(2100, 1, 1).getTime(),
    show: false,
    showDate:false,
    disabled:false,
    free: '',
    name: '',
    actions: [{
        name: '未接种',
        status: 0
      },
      {
        name: '已接种',
        status: 1
      },
    ],
    id:'',//计划id
    babyPlanData:{},
    babyId: '',
    description: '',
    planTime: '',
    status: '',
    planDate:'',
    currentDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    }
  },
  onDateClose(){
    this.setData({
      showDate: false
    })
  },
  //修改接种日期
  onConfirm(event) {
    let planDate = utils.formatDate(new Date(event.detail))
    utils.HttpRequst(false, '/baby-cloud-base/mobileVaccinePlan/editItem', 'post', 
    {
      babyId: this.data.babyId,
      id: this.data.id,
      status: this.data.status,
      planTime: planDate
    },res=>{
      if (res.data.code == 200) {
        Toast('修改成功')
        this.setData({
          planDate: planDate
        })
        console.log(this.data)

      } else {
        Toast(res.data.message);
      }
    })
    this.setData({
      showDate: false
    })
  },
  //修改接种日期弹框
  dateFocus() {
    this.setData({
      showDate: true
    })
  },
  
  //修改接种状态弹框
  statusFocus(){
    let that = this;
    let planDate = that.data.planDate;
    let noeDate = new Date().getTime();
    let plan = new Date(planDate).getTime();
    if (plan > noeDate){ //只能选择未接种状态不能选择已接种状态
      this.setData({
        show: true,
        actions: [{
          name: '未接种',
          status: 0
        },
        {
          name: '已接种',
          status: 1,
        },
        ],
      })
    }
    this.setData({
      show: true
    })
  },
  //修改接种状态
  onSelect(e) {
    let that=this
    let currentDate = new Date().getTime();
    let newPlanDate = new Date(that.data.planDate.replace(/-/g, "/")).getTime();
    console.log(currentDate)
    console.log(newPlanDate)
    console.log(e.detail)
    let timeCha = parseInt((currentDate - newPlanDate) / (1000 * 60 * 60 * 24));
    let title ="修改接种状态"
    let content='确认修改接种状态？'
    if(timeCha<0&&e.detail.status==1){//未到接种时间的疫苗
      content= '该疫苗未到接种时间，确认修改疫苗接种状态？'
    } else if (timeCha > 0 && e.detail.status == 1){
      content = '确认已接种该疫苗，并且修改接种状态？'
    }
    wx.showModal({
      title: title,
      content: content,
      success(res1) {
        if (res1.confirm) {
          let data = {
            babyId: that.data.babyId,
            id: that.data.id,
            status: e.detail.status,
            planTime: that.data.planDate
          }
          that.changeData(data)
        }
      }
    })
  },
  //确认修改接种状态
  changeData(data){
    utils.HttpRequst(false,'/baby-cloud-base/mobileVaccinePlan/editItem','post',data,res=>{
      if (res.data.code == 200) {
        Toast('修改成功');
        if (data.status == 1){
          this.setData({
            status: data.status,
            maxDate: new Date().getTime()
          })
        }else{
          this.setData({
            status: data.status,
            maxDate:new Date(2100, 1, 1).getTime()
          })
        }
        
      } else {
        Toast(res.data.message);
      }
    })
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  getDetail(){
    utils.HttpRequst(true, '/baby-cloud-base/mobileVaccinePlan/detail', 'post', { id: this.data.id, babyId: this.data.babyId }, res => {
      console.log(res.data.data)
      let data=res.data.data
      if (res.data.code == 200) {
        console.log(data.status)
        if (data.status == 0){
          this.setData({
            babyId: data.babyId,
            id: data.id,
            maxDate: new Date(2100, 1, 1).getTime(),
            description: data.description,
            planTime: data.planTime,
            planDate: data.planTime.split(' ')[0],
            status: data.status,
            free: data.free,
            name: data.name
          })
        } else if (data.status == 1){
          this.setData({
            babyId: data.babyId,
            id: data.id,
            maxDate: new Date().getTime(),
            description: data.description,
            planTime: data.planTime,
            planDate: data.planTime.split(' ')[0],
            status: data.status,
            free: data.free,
            name: data.name
          })
        }
       
      }
      console.log(this.data)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id,
      babyId: options.babyId
    })
    this.getDetail()
  }
})